package com.harmontronics.hanma.paas.paas.domain.app.service.dialect;

import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.domain.app.repository.po.HmModelPO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author ldb
 * 默认的实现以pg为准
 */
public interface DialectQuery {

     List<Map<String,Object>> list(HmModelPO modelPO,Map<String, Object> params, List<String> orderRules) throws SQLException;
     PageBase<Map<String,Object>> pageList(HmModelPO modelPO,Map<String, Object> params, List<String> orderRules,Integer pageNo, Integer pageSize) throws SQLException;

     String getSelectColumns(HmModelPO modelPO);

    default String getWhereStr(Map<String,Object> params){
        String whereStr = "1=1";
        if(CollectionUtils.isEmpty(params)){
            return whereStr;
        }
        StringBuilder builder=new StringBuilder(whereStr);
        for(Map.Entry<String,Object> entry:params.entrySet()){
            if(!valid(entry.getValue())){
                continue;
            }
            builder.append(" and ");
            builder.append(entry.getKey());
            builder.append("=");
            builder.append(getValue(entry.getValue()));
        }
        return builder.toString();
    }

    default boolean valid(Object value){
        if(value == null){
            return false;
        }
        if(value instanceof String){
            String str = (String)value;
            if(StringUtils.isEmpty(str)){
                return false;
            }
        }
        return true;
    }

    default String getValue(Object value) {
        if (value == null) {
            return "null";
        }
        if (value instanceof Integer || value instanceof Long || value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
            return value.toString();
        }
        if (value instanceof LocalDateTime) {
            return "'" + ((LocalDateTime) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "'";
        }
        if (value instanceof LocalDate) {
            return "'" + ((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "'";
        }
        if (value instanceof LocalTime) {
            return "'" + ((LocalTime) value).format(DateTimeFormatter.ofPattern("HH:mm:ss")) + "'";
        }
        return "'" + value.toString() + "'";
    }

    default String getOrderByStr(List<String> orderRules){
        if(CollectionUtils.isEmpty(orderRules)){
            return "";
        }
        StringJoiner joiner=new StringJoiner(",");
        orderRules.forEach(joiner::add);
        return "order by "+joiner.toString();
    }


    default List<Map<String, Object>> toListMap(ResultSet ret) throws SQLException {
        List<Map<String, Object>> list = new ArrayList();
        ResultSetMetaData meta = ret.getMetaData();
        int cot = meta.getColumnCount();

        while(ret.next()) {
            Map<String, Object> map = new HashMap();
            for(int i = 0; i < cot; i++) {
                map.put(meta.getColumnName(i + 1), ret.getObject(i + 1));
            }
            list.add(map);
        }

        return list;
    }


}
